﻿from google.appengine.ext import db

DEFAULT_LANG = 'rus'

class LocalizationLabel(db.Model):
	name = db.StringProperty(required=True)
	
class LocalizationLang(db.Model):
	name = db.StringProperty(required=True)
	description = db.StringProperty()
	
class LocalizationTranslation(db.Model):
	label = db.ReferenceProperty(LocalizationLabel)
	lang = db.ReferenceProperty(LocalizationLang)
	translation = db.StringProperty()


g_categories = [u"Антикварная вещица", u"Естонский товар", u"Грузинский товар"]
class Product(db.Model):
	name = db.ReferenceProperty(LocalizationLabel, required=True)
	description = db.StringProperty(multiline=True)
	price = db.FloatProperty()
	date = db.DateTimeProperty(auto_now_add=True)
	rating = db.RatingProperty()
	count = db.IntegerProperty()
	category = db.StringProperty(choices=set(g_categories))#db.ReferenceProperty(ItemCategory)
	image_path = db.StringProperty()
	
	def name_loc(self):
		transObj = LocalizationTranslation.all().filter('label = ', self.name).filter('lang = ', self.lang).get()
		translation = ''
		if transObj==None:
			translation = "[" + self.name.name + "]"
		else:
			translation = transObj.translation
		return translation

	def description_loc(self):
		return ""
	
	def translate(self, lang=DEFAULT_LANG):
		self.lang = LocalizationLang.all().filter('name = ', lang).get()
		if self.lang == None:
			self.lang = LocalizationLang.all().filter('name = ', DEFAULT_LANG).get()
		return self;



def createTranslations():
	flushModel(Product)
	flushModel(LocalizationTranslation)
	flushModel(LocalizationLang)
	flushModel(LocalizationLabel)
	#deleteAllEntities("Product")


	#label const
	lcBoots = u'COOL_BOOTS'
	lcFork = u'GOLDEN_FORK'
	lcGShirt = u'G_SHIRT'
	#labels
	lBoots = LocalizationLabel(name=lcBoots)
	lBoots.put()
	lFork = LocalizationLabel(name=lcFork)
	lFork.put()
	lGShirt = LocalizationLabel(name=lcGShirt)
	lGShirt.put()

	#langs
	rus = LocalizationLang(name='rus')
	rus.put()
	eng = LocalizationLang(name='eng')
	eng.put()
	pt = LocalizationLang(name='pt')
	pt.put()
	

	#translations
	translate(rus, lFork, u'Золотая вилочка')
	translate(rus, lBoots, u"Туфли охуєнниє")
	translate(rus, lGShirt, u"Маєчка")
	translate(eng, lFork, u'Golden forky')
	translate(eng, lBoots, u"Boots")
	return {lcBoots: lBoots, lcFork:lFork, lcGShirt:lGShirt}
	
def translate(lang, label, translation):
	trans = LocalizationTranslation(lang=lang, label=label, translation=translation)
	trans.put()


def createProducts():
	trans_labels = createTranslations()
	#return (trans_labels['COOL_BOOTS'], trans_labels['GOLDEN_FORK'])
	return [createProduct(trans_labels[u'COOL_BOOTS'], 2, 15.34, 5, 'superboots.gif'), 
			createProduct(trans_labels[u'GOLDEN_FORK'], 2, 2.00, 5, 'goldenfork.gif'),
			createProduct(trans_labels[u'G_SHIRT'], 2, 2.00, 5, 'goldenfork.gif'),
			]


def flushModel(modelClass):
	for entity in modelClass.all():
		entity.delete()
		
def deleteAllEntities(table):
        q = db.GqlQuery("SELECT * FROM "+table)
        results = q.fetch(100)
        while len(results) > 0:
                for result in results:
                        result.delete()
                q = db.GqlQuery("SELECT * FROM "+table)
                results = q.fetch(100) 



def createProduct(locLabel, category_id, price, rating, image=''):
	product = Product(name=locLabel,
					category=g_categories[category_id],
					price = price,
					rating = rating,
					image_path = image
					)
	product.put()
	return product